package com.luo.backend.controller;

import com.luo.backend.common.ApiResponse;
import com.luo.backend.entity.User;
import com.luo.backend.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 调试控制器 - 仅用于开发环境
 */
@RestController
@RequestMapping("/api/v1/debug")
public class DebugController {
    
    private static final Logger logger = LoggerFactory.getLogger(DebugController.class);
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private PasswordEncoder passwordEncoder;
    
    /**
     * 创建测试用户
     */
    @PostMapping("/create-test-user")
    public ApiResponse<Map<String, Object>> createTestUser() {
        try {
            // 检查是否已存在
            if (userRepository.findByUsername("testuser").isPresent()) {
                return ApiResponse.error("测试用户已存在");
            }
            
            // 创建测试用户
            User user = new User();
            user.setUsername("testuser");
            user.setEmail("test@example.com");
            user.setPasswordHash(passwordEncoder.encode("123456"));
            user.setPhone("13800138000");
            user.setRealName("测试用户");
            user.setStatus(1);
            
            user = userRepository.save(user);
            
            Map<String, Object> result = new HashMap<>();
            result.put("id", user.getId());
            result.put("username", user.getUsername());
            result.put("email", user.getEmail());
            result.put("passwordHash", user.getPasswordHash());
            
            return ApiResponse.success("测试用户创建成功", result);
            
        } catch (Exception e) {
            logger.error("创建测试用户失败", e);
            return ApiResponse.error("创建失败: " + e.getMessage());
        }
    }
    
    /**
     * 重置测试用户密码
     */
    @PostMapping("/reset-test-password")
    public ApiResponse<String> resetTestPassword() {
        try {
            User user = userRepository.findByUsername("testuser").orElse(null);
            if (user == null) {
                return ApiResponse.error("测试用户不存在");
            }
            
            // 重新设置密码
            user.setPasswordHash(passwordEncoder.encode("123456"));
            userRepository.save(user);
            
            logger.info("测试用户密码已重置 - 新密码哈希: {}", user.getPasswordHash());
            
            return ApiResponse.success("测试用户密码重置成功: " + user.getPasswordHash());
            
        } catch (Exception e) {
            logger.error("重置密码失败", e);
            return ApiResponse.error("重置失败: " + e.getMessage());
        }
    }
    
    /**
     * 查看用户信息
     */
    @GetMapping("/user/{username}")
    public ApiResponse<Map<String, Object>> getUserInfo(@PathVariable String username) {
        try {
            User user = userRepository.findByUsername(username).orElse(null);
            if (user == null) {
                return ApiResponse.error("用户不存在");
            }
            
            Map<String, Object> result = new HashMap<>();
            result.put("id", user.getId());
            result.put("username", user.getUsername());
            result.put("email", user.getEmail());
            result.put("passwordHash", user.getPasswordHash());
            result.put("status", user.getStatus());
            
            return ApiResponse.success(result);
            
        } catch (Exception e) {
            logger.error("查询用户失败", e);
            return ApiResponse.error("查询失败: " + e.getMessage());
        }
    }
}




